const express = require("express");

const router = express.Router();

const db = require("../db");

router.get("/check-username", async (req, res) => {
  // 获取get请求中的username的内容
  const { username } = req.query;

  // 检查必要的参数
  if (!username?.trim()) {
    return res
      .status(400)
      .json({ status: "fail", message: "没有携带必要的参数：username" });
  }

  // 在数据库进行查找
  const result = await db.findOneUser({ username });

  if (result) {
    return res.json({ status: "fail", message: "该用户已存在" });
  }
  res.json({ status: "success", message: "该用户名未注册" });
});

router.get("/post", async (req, res) => {
  const { page = 0, limit = 5 } = req.query;
  const posts = await db.findPost(
    {},
    { limit: Number(limit), skip: page * limit }
  );
  res.json(posts);
});

module.exports = router;
